<template>
  <div class="admin-layout">
    <el-container class="layout-container">
      <!-- 顶部导航栏 -->
      <el-header class="layout-header">
        <div class="header-content">
          <div class="logo">
            <el-icon><Setting /></el-icon>
            <span>银行管理系统 - 管理员</span>
          </div>
          <div class="header-actions">
            <el-button 
              type="text" 
              class="collapse-btn"
              @click="toggleSidebar"
            >
              <el-icon>
                <component :is="isCollapse ? 'Expand' : 'Fold'" />
              </el-icon>
            </el-button>
            <el-badge :value="unreadCount" :hidden="unreadCount === 0">
              <el-button 
                type="text" 
                class="notification-btn"
                @click="showNotifications"
              >
                <el-icon><Bell /></el-icon>
              </el-button>
            </el-badge>
          </div>
          <div class="user-info">
            <span>管理员：{{ userInfo?.username || 'Admin' }}</span>
            <el-dropdown @command="handleCommand">
              <el-avatar :src="userInfo?.imageurl" class="user-avatar">
                <el-icon><UserFilled /></el-icon>
              </el-avatar>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <el-container>
        <!-- 侧边栏 -->
        <el-aside class="layout-aside" :class="{ collapsed: isCollapse }">
          <el-menu
            :default-active="activeMenu"
            :collapse="isCollapse"
            :unique-opened="true"
            class="sidebar-menu"
            router
          >
            <el-menu-item index="/admin/dashboard">
              <el-icon><House /></el-icon>
              <template #title>仪表板</template>
            </el-menu-item>
            
            <el-menu-item index="/admin/customers">
              <el-icon><User /></el-icon>
              <template #title>客户管理</template>
            </el-menu-item>
            
            <el-menu-item index="/admin/cards">
              <el-icon><CreditCard /></el-icon>
              <template #title>银行卡管理</template>
            </el-menu-item>
            
            <el-menu-item index="/admin/deposits">
              <el-icon><Money /></el-icon>
              <template #title>存款类型</template>
            </el-menu-item>
            
            <el-menu-item index="/admin/trades">
              <el-icon><TrendCharts /></el-icon>
              <template #title>交易管理</template>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <!-- 主内容区 -->
        <el-main class="layout-main">
          <router-view />
        </el-main>
      </el-container>
    </el-container>

    <!-- 通知抽屉 -->
    <el-drawer
      v-model="notificationDrawer"
      title="系统通知"
      direction="rtl"
      size="400px"
    >
      <div class="notifications">
        <div v-if="notifications.length === 0" class="no-notifications">
          <el-icon><Bell /></el-icon>
          <p>暂无新通知</p>
        </div>
        
        <div v-else>
          <div 
            v-for="notification in notifications" 
            :key="notification.id"
            class="notification-item"
            :class="{ 'unread': !notification.read }"
            @click="markAsRead(notification)"
          >
            <div class="notification-icon">
              <el-icon v-if="notification.type === 'info'"><InfoFilled /></el-icon>
              <el-icon v-else-if="notification.type === 'warning'"><WarningFilled /></el-icon>
              <el-icon v-else-if="notification.type === 'error'"><CircleCloseFilled /></el-icon>
              <el-icon v-else><SuccessFilled /></el-icon>
            </div>
            <div class="notification-content">
              <div class="notification-title">{{ notification.title }}</div>
              <div class="notification-message">{{ notification.message }}</div>
              <div class="notification-time">{{ formatTime(notification.time) }}</div>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  House,
  User,
  CreditCard,
  Money,
  TrendCharts,
  Document,
  Setting,
  Tools,
  Expand,
  Fold,
  Bell,
  InfoFilled,
  WarningFilled,
  CircleCloseFilled,
  SuccessFilled,
  UserFilled
} from '@element-plus/icons-vue'

export default {
  name: 'AdminLayout',
  components: {
    House,
    User,
    CreditCard,
    Money,
    TrendCharts,
    Document,
    Setting,
    Tools,
    Expand,
    Fold,
    Bell,
    InfoFilled,
    WarningFilled,
    CircleCloseFilled,
    SuccessFilled,
    UserFilled
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    
    const isCollapse = ref(false)
    const notificationDrawer = ref(false)
    
    // 用户信息
    const userInfo = computed(() => store.state.userInfo)
    
    // 当前激活的菜单
    const activeMenu = computed(() => route.path)
    
    // 通知数据
    const notifications = ref([
      {
        id: 1,
        type: 'info',
        title: '系统升级通知',
        message: '系统将于今晚22:00-24:00进行升级维护',
        time: new Date(),
        read: false
      },
      {
        id: 2,
        type: 'warning',
        title: '异常交易提醒',
        message: '检测到大额交易，请及时关注',
        time: new Date(Date.now() - 3600000),
        read: false
      },
      {
        id: 3,
        type: 'success',
        title: '备份完成',
        message: '数据库备份已成功完成',
        time: new Date(Date.now() - 7200000),
        read: true
      }
    ])

    // 未读通知数量
    const unreadCount = computed(() => 
      notifications.value.filter(n => !n.read).length
    )
    
    // 切换侧边栏
    const toggleSidebar = () => {
      isCollapse.value = !isCollapse.value
    }
    
    // 显示通知
    const showNotifications = () => {
      notificationDrawer.value = true
    }

    // 标记为已读
    const markAsRead = (notification) => {
      notification.read = true
    }
    
    // 处理用户菜单命令
    const handleCommand = async (command) => {
      switch (command) {
        case 'profile':
          router.push('/admin/profile')
          break
        case 'logout':
          await handleLogout()
          break
      }
    }
    
    // 退出登录
    const handleLogout = async () => {
      try {
        await ElMessageBox.confirm(
          '确定要退出登录吗？',
          '退出确认',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        
        store.dispatch('logout')
        router.push('/login')
        ElMessage.success('已退出登录')
      } catch (error) {
        // 用户取消
      }
    }
    
    // 格式化时间
    const formatTime = (time) => {
      const now = new Date()
      const diff = now - time
      const minutes = Math.floor(diff / 60000)
      const hours = Math.floor(diff / 3600000)
      const days = Math.floor(diff / 86400000)
      
      if (minutes < 1) return '刚刚'
      if (minutes < 60) return `${minutes}分钟前`
      if (hours < 24) return `${hours}小时前`
      if (days < 30) return `${days}天前`
      return time.toLocaleDateString()
    }
    
    onMounted(() => {
      // 检查用户权限
      if (!userInfo.value?.id) {
        router.push('/login')
      }
    })
    
    return {
      isCollapse,
      notificationDrawer,
      userInfo,
      activeMenu,
      notifications,
      unreadCount,
      toggleSidebar,
      showNotifications,
      markAsRead,
      handleCommand,
      formatTime
    }
  }
}
</script>

<style lang="scss" scoped>
.admin-layout {
  height: 100vh;

  .layout-container {
    height: 100%;
  }

  .layout-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    display: flex;
    align-items: center;
    padding: 0 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;

      .logo {
        display: flex;
        align-items: center;
        font-size: 20px;
        font-weight: bold;

        .el-icon {
          margin-right: 10px;
          font-size: 28px;
        }
      }

      .header-actions {
        display: flex;
        align-items: center;
        gap: 15px;

        .collapse-btn,
        .notification-btn {
          color: white;
          font-size: 18px;
          
          &:hover {
            background-color: rgba(255, 255, 255, 0.1);
          }
        }
      }

      .user-info {
        display: flex;
        align-items: center;
        gap: 12px;
        font-size: 14px;

        .user-avatar {
          cursor: pointer;
          transition: transform 0.2s;

          &:hover {
            transform: scale(1.05);
          }
        }
      }
    }
  }
  
  .layout-aside {
    width: 240px !important;
    transition: width 0.3s;
    background: #fff;
    box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);

    &.collapsed {
      width: 64px !important;
    }
    
    .sidebar-menu {
      border: none;
      height: calc(100vh - 60px);
      
      .el-menu-item {
        margin: 4px 12px;
        border-radius: 8px;
        
        &:hover {
          background-color: #f0f9ff !important;
          color: #409eff !important;
        }
        
        &.is-active {
          background: linear-gradient(135deg, #409eff, #36cfc9) !important;
          color: white !important;
          
          .el-icon {
            color: white !important;
          }
        }
      }
      
      .el-sub-menu {
        margin: 4px 12px;
        border-radius: 8px;
        
        .el-sub-menu__title {
          border-radius: 8px;
          
          &:hover {
            background-color: #f0f9ff !important;
            color: #409eff !important;
          }
        }

        .el-menu-item {
          margin: 2px 8px;
        }
      }
    }
  }
  
  .layout-main {
    background: #f5f7fa;
    padding: 0;
    overflow-y: auto;
  }
}

.notifications {
  .no-notifications {
    text-align: center;
    padding: 60px 20px;
    color: #909399;
    
    .el-icon {
      font-size: 48px;
      margin-bottom: 16px;
    }
  }
  
  .notification-item {
    display: flex;
    padding: 16px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    transition: background-color 0.2s;
    
    &:hover {
      background-color: #f8f9fa;
    }
    
    &.unread {
      background-color: #f0f9ff;
      
      .notification-title {
        font-weight: bold;
      }
    }
    
    .notification-icon {
      margin-right: 12px;
      font-size: 20px;
      
      .el-icon {
        &[class*="info"] { color: #409eff; }
        &[class*="warning"] { color: #e6a23c; }
        &[class*="error"] { color: #f56c6c; }
        &[class*="success"] { color: #67c23a; }
      }
    }
    
    .notification-content {
      flex: 1;
      
      .notification-title {
        font-size: 14px;
        color: #303133;
        margin-bottom: 4px;
      }
      
      .notification-message {
        font-size: 12px;
        color: #606266;
        margin-bottom: 8px;
        line-height: 1.4;
      }
      
      .notification-time {
        font-size: 12px;
        color: #909399;
      }
    }
  }
}
</style> 